package com.wwdx.controller.admin;

import com.wwdx.controller.base.BaseController;
import com.wwdx.model.WwdxCdKeyModel;
import com.wwdx.model.WwdxMerchantModel;
import com.wwdx.model.dto.ResultBean;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;

/**
 * @author liguoliang
 * @date 2017/12/20 18:50
 */
@Controller
@RequestMapping("/system/wwdx_merchant")
public class WwdxMerchantController extends BaseController{

    /**
     * 列表
     */
    @RequestMapping("/list")
    public ModelAndView index(WwdxMerchantModel model,
                              @RequestParam(value = "page", defaultValue = "1") Integer page,
                              @RequestParam(value = "rows",defaultValue = "10") Integer rows) {

        ModelAndView mav = new ModelAndView();
        mav.addObject("listSize", wwdxMerchantService.selectModelCount(model));
        mav.addObject("list", wwdxMerchantService.selectModelList(model, page, rows));
        return mav;
    }

    @RequestMapping("/list/json")
    @ResponseBody
    public ResultBean listjson() {
        return renderSuccess(wwdxMerchantService.selectModelList(null, 1, 100));
    }

    /**
     * 新增
     */
    @RequestMapping("/create")
    public ModelAndView create() {
        return new ModelAndView();
    }

    /**
     * 保存新增
     */
    @RequestMapping("/save")
    public String save(WwdxMerchantModel model) {
        model.setGmt_created(new Date());
        model.setGmt_modified(new Date());
        model.setId(super.getUUID());
        wwdxMerchantService.save(model);
        return "redirect:/system/wwdx_merchant/list";
    }

    /**
     * 修改页面
     */
    @RequestMapping("/edit")
    public ModelAndView edit(String id) {
        ModelAndView mav = new ModelAndView();
        mav.addObject("model", wwdxMerchantService.getById(id));
        return mav;
    }

    /**
     * 保存修改
     */
    @RequestMapping("/update")
    public String update(WwdxMerchantModel model) {
        model.setGmt_modified(new Date());
        wwdxMerchantService.update(model);
        return "redirect:/system/wwdx_merchant/list";
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresRoles("super")
    public String delete(String id) {
        wwdxMerchantService.delete(wwdxMerchantService.getById(id));
        return "redirect:/system/wwdx_merchant/list";
    }

    /**
     * 添加兑换码
     */
    @RequestMapping("/cdkey_create")
    public ModelAndView cdkeyCreate() {
        return new ModelAndView();
    }

    /**
     * 生成兑换码
     */
    @RequestMapping("/cdkey_save")
    public String save(String merchantId,Long productId,Integer num){
        WwdxCdKeyModel wwdxCDKEYModel = WwdxCdKeyModel.builder()
                .product_id(productId).type(7).merchant_id(merchantId).build();
        wwdxCdKeyService.addCDKEY(wwdxCDKEYModel, num);
        return "redirect:/system/wwdx_merchant/list";
    }
}